<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\common\model\TaskModel;
use crontab\api\Action;
use think\facade\Db;

class Crontab extends BaseController
{
    public function index()
    {
        if ($this->request->isPost()) {
            $search = $this->request->post('search');
            if(!empty($search)){
                $where = [];
                if($search['name']){
                    $where[] = ['name','LIKE', $search['name'].'%'];
                }
                if($search['status'] != ''){
                    $where[] = ['status','=',$search['status']];
                }
                $list = TaskModel::where($where)->paginate()->append(['status_txt']);
                $this->jsonjqgrid($list->items(), $list->total());
            }
        }
        $this->assign('status',TaskModel::$statusArr);
        return $this->fetch();
    }
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            try {
                $this->validate($param, 'CrontabV.add');
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            Db::startTrans();
            try {
                $taskDao = TaskModel::create($param);
                $req['id'] = $taskDao->id;
                $req['crontab'] = $param['rule'];
                if (Action::add($req)) {
                    Db::commit();
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('添加成功');
        }
         $this->assign('types',TaskModel::$exec_type_arr);
        return $this->fetch();
    }

    public function edit($id)
    {
        $data = TaskModel::find($id);
        if ($this->request->isPost()) {
            $param = $this->request->param();
            try {
                $this->validate($param, 'CrontabV.add');
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            Db::startTrans();
            try {
                $data->save($param);
                $req['id'] = $data->id;
                $req['crontab'] = $data->rule;
                if (Action::add($req)) {
                    Db::commit();
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('编辑成功');

        }
        $this->assign('data',$data);
        return $this->fetch('',[
            'data'=>$data,
            'types' =>TaskModel::$exec_type_arr,
        ]);
    }
    
    //执行
    public function  exec($id){
        try {
            $res = Action::run(['id'=>$id]);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('执行成功');  
    }

    //暂停
    public function  suspend($id){
        $data = TaskModel::find($id);
        Db::startTrans();
        try {
            $data->status = 1;
            $data->save();
            $req['id'] = $data->id;
            if (Action::suspend($req)) {
                Db::commit();
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('暂停成功');
    }

    //启动任务
    public function  start($id){
        $data = TaskModel::find($id);
        Db::startTrans();
        try {
            $data->status = 2;
            $data->save();
            $req['id'] = $data->id;
            if (Action::start($req)) {
                Db::commit();
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('启动成功');
    }


    //删除任务
    public function  delete($id){
        $data = TaskModel::find($id);
        Db::startTrans();
        try {
            $data->delete();
            $req['id'] = $data->id;
            if (Action::delete($req)) {
                Db::commit();
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('启动成功');
    }


}
